home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / diff.zip / CMDUTL.C next >
Text File  |  1986-03-11  |  4KB  |  147 lines

  1.  
  2. /*  K**2 subroutine library.    */
  3.  
  4. /*  Functions included:
  5. abbrev      Test for a valid abbreviation.
  6. initv       Initialize an integer vector.
  7. procarg     Process an argument from command line.
  8. showsynt    Display the correct syntax for a command.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include "cmdutil.h"
  14.  
  15. /*  ABBREV - Test for a valid abbreviation */
  16.  
  17. abbrev (text, pattern)
  18.     char * text;        /* Text to test */
  19.     char * pattern;     /* Pattern to match it against; lowercase
  20.                     letters are optional */
  21.     {
  22.     while (*pattern != '\0') {
  23.         if (islower (*pattern)) {   /* Optional character */
  24.             if (tolower (*text) == *pattern++
  25.              && abbrev (text + 1, pattern))
  26.                 return (TRUE);
  27.             }
  28.         else {              /* Required character */
  29.             if (toupper (*text++) != *pattern++)
  30.                 return (FALSE);
  31.             }
  32.         }
  33.     return (*text == '\0');
  34.     }
  35.  
  36. /*  Process an argument from command line   */
  37.  
  38. int procarg (argc, argv, optable, info) 
  39.     int * argc;         /* Argument count */
  40.     char * * * argv;        /* Argument vector */
  41.     struct option * optable;    /* Option list */
  42.     char * * * info;        /* Returned information */
  43.     {
  44.     int optno;      /* Option number */
  45.         char * argtext;     /* Argument text */
  46.     int parmno;     /* Parameter counter for multi-value
  47.                    options. */
  48.     
  49.     argtext = *(*argv)++;       /* Pick up an argument */
  50.     --*argc;
  51.  
  52.     if (!_isopt (argtext)) {
  53.         *info = argtext;    /* String, not an option */
  54.         return (-1);
  55.         }
  56.  
  57.     for (optno = 0; optable -> opt_text != EOF; ++optno, ++optable)
  58.         if (abbrev (argtext + 1, optable -> opt_text)) break;
  59.                 /* Search for optable entry */
  60.  
  61.     if (optable -> opt_text == EOF) {
  62.         fprintf (stderr, "\n-%s: unknown option.", argtext);
  63.         *info = argtext;
  64.         return (-2);        /* Unrecognized option */
  65.         }
  66.  
  67.     switch (optable -> opt_type) {
  68.         case NAKED_KWD:
  69.         *info = 0;
  70.         return (optno);
  71.         case NVAL_KWD:
  72.         case SVAL_KWD:
  73.         case MNVL_KWD:
  74.         case MSVL_KWD:
  75.         if (*argc == 0 || _isopt (**argv)) {
  76.             fprintf (stderr, "\n-%s option requires a value.", 
  77.                 optable -> opt_text);
  78.             *info = argtext;
  79.             return (-2);
  80.             }
  81.         break;
  82.         default:
  83.         fprintf (stderr, "\nBug: optable badly constructed.");
  84.         exit ();
  85.         }
  86.  
  87.     switch (optable -> opt_type) {
  88.         case SVAL_KWD:
  89.         *info = *(*argv)++; /* Pick up next arg string */
  90.         --*argc;
  91.             return (optno);
  92.         case NVAL_KWD:
  93.         *info = atoi (*(*argv)++);
  94.         --*argc;
  95.         return (optno);
  96.         default:
  97.         *info = sbrk (*argc + *argc + 2);
  98.         break;
  99.         }
  100.  
  101.     for (parmno = 0; 
  102.         *argc && !_isopt (argtext = **argv);
  103.         --*argc, ++*argv, ++parmno) {
  104.  
  105.         if (optable -> opt_type == MSVL_KWD) {
  106.             (*info) [parmno + 1] = argtext;
  107.             }
  108.         else {
  109.             (*info) [parmno + 1] = atoi (argtext);
  110.             }
  111.         }
  112.     (*info) [0] = parmno;
  113.     return (optno);
  114.     }
  115.  
  116. /* Test if a string is a command option */
  117.  
  118. int _isopt (s)
  119.     char * s;
  120.     {
  121.     return (*s++ == '-' && isalpha (*s));
  122.     }
  123.  
  124. /* Display command syntax */
  125.  
  126. showsyntax (command, optable)
  127.     char * command;
  128.     struct option * optable;
  129.     {
  130.     static char * opstr [6] = {
  131.         "", "<s>", "<n>", "<s> <s>...", "<n> <n>...", EOF };
  132.  
  133.     fprintf (stderr, "\nSyntax: %s", command);
  134.  
  135.     if (optable -> opt_text == EOF) return;
  136.  
  137.     fprintf (stderr, " <options>\nOptions:");
  138.  
  139.     while (optable -> opt_text != EOF) {
  140.  
  141.         fprintf (stderr, "\n\t-%s %s", optable -> opt_text,
  142.             opstr [optable -> opt_type]);
  143.         ++optable;
  144.  
  145.         }
  146.     }
  147.